#ifndef __UTIL_H__
#define __UTIL_H__
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <time.h>
#include <sys/time.h>

class CTools
{
    public:
        static int64_t GetTickCount()
        {
            timeval timesnow;
            struct timezone tmpzone;
            gettimeofday(&timesnow,&tmpzone);
            return ((int64_t)(timesnow.tv_sec*1000) + (int64_t)(timesnow.tv_usec/1000));
        }

        static int GetUnixTime()
        {
            timeval timesnow;
            struct timezone tmpzone;
            gettimeofday(&timesnow,&tmpzone);
            return ((int)(timesnow.tv_sec));
        }

        static uint64_t GetUnixTimeMs()
        {
            timeval timesnow;
            gettimeofday(&timesnow, NULL);
            return (timesnow.tv_sec * 1000000 + timesnow.tv_usec);
        }

        static int64_t GetUnixTimeNs() {
            timeval timesnow;
            gettimeofday(&timesnow, NULL);
            return (timesnow.tv_sec * 1000LL + timesnow.tv_usec / 1000LL);
        }
		
		static int GetDateStr(std::string &str)
		{
			char now[64] = {0};
			time_t tt = time(NULL);
			struct tm *ttime;
			ttime = localtime(&tt);
			strftime(now, sizeof(now), "%Y%m%d", ttime);
            str = now;
			return 0;
		}

        static int split_str(const char* ps_str, const char* ps_sp, vector<std::string> &v_ret)
        {
            char* ps_temp;
            char* p;
            int i_len = (int)strlen(ps_str);
            std::string st_str;
            ps_temp = new char[i_len + 2];
            snprintf(ps_temp, i_len + 1, "%s", ps_str);
            char *last = NULL;    p = strtok_r(ps_temp, ps_sp, &last);
            if (NULL == p)
            {
                delete ps_temp;
                return 0;
            }
            st_str = (std::string)p;
            v_ret.push_back(st_str);
            while (NULL != (p = strtok_r(NULL, ps_sp, &last)))
            {
                st_str = (std::string)p;
                v_ret.push_back(st_str);
            }
            delete ps_temp;
            return 0;
        }

        static bool isSameDay(time_t time1, time_t time2)
        {
            return (((time1 + 28800) / 86400) == ((time2 + 28800) / 86400));
        }

        static bool isSameWeek(time_t time1, time_t time2)
        {
            std::tm* local_tm = std::localtime(&time1);
            int week1 = local_tm->tm_wday == 0 ?7:local_tm->tm_wday;
            int year_week1 = local_tm->tm_year + ((local_tm->tm_yday + 1) + 6 - week1) / 7;
            local_tm = std::localtime(&time2);
            int week2 = local_tm->tm_wday == 0 ?7:local_tm->tm_wday;
            int year_week2 = local_tm->tm_year + ((local_tm->tm_yday + 1) + 6 - week2) / 7;
            return year_week1 == year_week2;
        }
        // 字符串转整数
        static int Hash2Int(const std::string& str, int max_num = 100)
        {
            int value = 0;
            std::string::const_iterator it = str.begin();
            for (; it != str.end(); it++)
            {
                value += *it;
            }
            return value % max_num;
        }
};

#endif